package com.example.canteen.service.impl;


import com.example.canteen.dao.CExceptionDao;
import com.example.canteen.dao.CanteenDao;
import com.example.canteen.dao.CanteenExceptionDao;
import com.example.canteen.dao.CanteenStatementDao;
import com.example.canteen.dao.impl.CExceptionDaoImpl;
import com.example.canteen.dao.impl.CanteenDaoImpl;
import com.example.canteen.dao.impl.CanteenExceptionDaoImpl;
import com.example.canteen.dao.impl.CanteenStatementDaoImpl;
import com.example.canteen.domain.CException;
import com.example.canteen.domain.Canteen;
import com.example.canteen.domain.CanteenStatement;
import com.example.canteen.service.CanteenService;

import java.util.List;

public class CanteenSerViceImpl implements CanteenService {
    private CanteenDao canteenDao = new CanteenDaoImpl();
    private CanteenStatementDao canteenStatementDao = new CanteenStatementDaoImpl();
    private CExceptionDao cExceptionDao = new CExceptionDaoImpl();
    private CanteenExceptionDao canteenExceptionDao = new CanteenExceptionDaoImpl();
//    @Override
//    public boolean save(Canteen canteen) {
//        Canteen c = canteenDao.findById(canteen.getId());
//        if(c!=null) {
//            return false;
//        }
//        CanteenStatement canteenStatement = canteenStatementDao.findByBookAndOpen(canteen.getCanteenStatement().getBookStatement(), canteen.getCanteenStatement().getOpenStatement());
//        if(canteenStatement==null) {
//            canteenStatementDao.save(canteen.getCanteenStatement());
//            canteenStatement = canteenStatementDao.findByBookAndOpen(canteen.getCanteenStatement().getBookStatement(), canteen.getCanteenStatement().getOpenStatement());
//        }
//        CException byTypeAndInfo=null;
//        for (CException cException : canteen.getcExceptions()) {
//            byTypeAndInfo = cExceptionDao.findByTypeAndInfo(cException.getExceptionType(), cException.getExceptionInfo());
//            if(byTypeAndInfo==null){
//                cExceptionDao.save(cException);
//                byTypeAndInfo=cExceptionDao.findByTypeAndInfo(cException.getExceptionType(), cException.getExceptionInfo());
//            }
//                canteenExceptionDao.save(canteen.getId(),byTypeAndInfo.getEid());
//        }
//
//        canteen.setCanteenStatement(canteenStatement);
//        canteenDao.save(canteen);
//        return true;
//    }
@Override
public boolean save(Canteen canteen) {
    Canteen c = canteenDao.findById(canteen.getId());
    if(c!=null) {
        return false;
    }

    canteenDao.save(canteen);
    return true;
}


//    @Override
//    public Canteen findOne(int id) {
//        Canteen canteen=null;
//        canteen=canteenDao.findById(id);
//        canteen.setCanteenStatement(canteenStatementDao.findById(canteen.getSid()));
//        canteen.setcExceptions(cExceptionDao.findByCanteenId(canteen.getId()));
//        return canteen;
//    }
@Override
public Canteen findOne(int id) {
    Canteen canteen=null;
    canteen=canteenDao.findById(id);
    return canteen;
}

//    @Override
//    public boolean update(Canteen canteen) {
//        if(canteenDao.update(canteen)==0)
//            return false;
//        canteenStatementDao.update(canteen.getCanteenStatement());
//        canteenExceptionDao.delete(canteen.getId());
//        for (CException cException : canteen.getcExceptions()) {
//            cExceptionDao.update(cException);
//            canteenExceptionDao.save(canteen.getId(),cException.getEid());
//        }
//
//        return true;
//    }
@Override
public boolean update(Canteen canteen) {
    if(canteenDao.update(canteen)==0)
        return false;
    return true;
}

//    @Override
//    public List<Canteen> findAll() {
//        List<Canteen> allCanteen = canteenDao.findAll();
////        System.out.println(allCanteen);
//        for (Canteen canteen : allCanteen) {
//            canteen.setCanteenStatement(canteenStatementDao.findById(canteen.getSid()));
//            canteen.setcExceptions(cExceptionDao.findByCanteenId(canteen.getId()));
//        }
//        return allCanteen;
//    }
    @Override
    public List<Canteen> findAll() {
        List<Canteen> allCanteen = canteenDao.findAll();
        return allCanteen;
    }
    @Override
    public boolean delete(int id) {
        if (canteenDao.delete(id)==0)
            return false;
        return true;
    }
}
